package com.example.lenovo.wechattest;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton BtnWechat;
    private ImageButton BtnContact;
    private ImageButton BtnFind;
    private ImageButton BtnMe;
    
    private WechatFragment WechatFragment = null;
    private ContactFragment ContactFragment = null;
    private FindFragment FindFragment = null;
    private MeFragment MeFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BtnWechat = findViewById(R.id.btn_wechat);
        BtnContact = findViewById(R.id.btn_contact);
        BtnFind = findViewById(R.id.btn_find);
        BtnMe = findViewById(R.id.btn_me);

        BtnWechat.setOnClickListener(this);
        BtnContact.setOnClickListener(this);
        BtnFind.setOnClickListener(this);
        BtnMe.setOnClickListener(this);
        
        selectTab(0);
    }

    private void selectTab(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if ( WechatFragment != null ) {
            transaction.hide(WechatFragment);
        }
        if ( ContactFragment != null ) {
            transaction.hide(ContactFragment);
        }
        if ( FindFragment != null ) {
            transaction.hide(FindFragment);
        }
        if ( MeFragment != null ) {
            transaction.hide(MeFragment);
        }

        switch (index) {
            case 0:
                if ( WechatFragment == null ) {
                    WechatFragment = new WechatFragment();
                    transaction.add(R.id.fl_contianer,WechatFragment);
                } else {
                    transaction.show(WechatFragment);
                }
                break;
            case 1:
                if ( ContactFragment == null ) {
                    ContactFragment = new ContactFragment();
                    transaction.add(R.id.fl_contianer,ContactFragment);
                } else {
                    transaction.show(ContactFragment);
                }
                break;
            case 2:
                if ( FindFragment == null ) {
                    FindFragment = new FindFragment();
                    transaction.add(R.id.fl_contianer,FindFragment);
                } else {
                    transaction.show(FindFragment);
                }
                break;
            case 3:
                if ( MeFragment == null ) {
                    MeFragment = new MeFragment();
                    transaction.add(R.id.fl_contianer,MeFragment);
                } else {
                    transaction.show(MeFragment);
                }
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.btn_wechat:
                BtnWechat.setImageResource(R.mipmap.tab_weixin_pressed);
                selectTab(0);
                break;
            case R.id.btn_contact:
                BtnContact.setImageResource(R.mipmap.tab_address_pressed);
                selectTab(1);
                break;
            case R.id.btn_find:
                BtnFind.setImageResource(R.mipmap.tab_find_frd_pressed);
                selectTab(2);
                break;
            case R.id.btn_me:
                BtnMe.setImageResource(R.mipmap.tab_settings_pressed);
                selectTab(3);
                break;
        }
    }

    private void setTabDefaultColor() {
        BtnWechat.setImageResource(R.mipmap.tab_weixin_normal);
        BtnContact.setImageResource(R.mipmap.tab_address_normal);
        BtnFind.setImageResource(R.mipmap.tab_find_frd_normal);
        BtnMe.setImageResource(R.mipmap.tab_settings_normal);
    }
}
